﻿using System;
using System.Data;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class AddComment : System.Web.UI.Page
    {
        private PositionBLL positionBll = new PositionBLL();
        private DimensionBLL dimensionBll = new DimensionBLL();
        private CommentBLL commentBll = new CommentBLL();
        
        private string dimensionID = string.Empty;
        private string positionID = string.Empty;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteSystemMaster)Master).InitMenu();
            ((SiteSystemMaster)Master).li3.Attributes.Add("class", "hover ccc");
            ((SiteSystemMaster)Master).b_hmenu1.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu3.Style.Clear();
            ((SiteSystemMaster)Master).s_menu3_3.Style.Add("COLOR", "#147ab8");
            ((SiteSystemMaster)Master).s_menu3_3.Style.Add("TEXT-DECORATION", "none"); 

            /*判断是否是管理员身份*/
            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('请先登录系统!');top.location.href='../System/Login.aspx';</script>");
                return;
            }
            else
            {
                if (!IsPostBack)
                {
                    databind();
                }
            }
        }

        private void databind()
        {
            dimensionID = Request.QueryString["dimension_id"].ToString();
            positionID = Request.QueryString["position_id"].ToString();

            DataSet dsPosition = positionBll.GetPositionAll(string.Empty);
            ddlPosition.DataSource = dsPosition.Tables[0].DefaultView;

            ddlPosition.DataTextField = "position_name";
            ddlPosition.DataValueField = "position_id";
            ddlPosition.DataBind();

            DataSet dsDimension = dimensionBll.GetDimensionAll();
            ddlDimension.DataSource = dsDimension.Tables[0].DefaultView;
            ddlDimension.DataValueField = "dimension_id";
            ddlDimension.DataTextField = "dimension_name";
            ddlDimension.DataBind();

            if (!string.IsNullOrEmpty(positionID))
            {
                ddlPosition.SelectedValue = positionID;
            }

            if (!string.IsNullOrEmpty(dimensionID))
            {
                ddlDimension.SelectedValue = dimensionID;
            }

            txtbComment.Text = commentBll.GetCommentByAll(int.Parse(ddlPosition.SelectedValue), 
                                                          int.Parse(ddlDimension.SelectedValue), 
                                                          int.Parse(ddlReportPart.SelectedValue), 
                                                          int.Parse(ddlRegion.SelectedValue));
        }

        private void dataupdate()
        {
            string strComment = string.Empty;
            strComment = commentBll.GetCommentByAll(int.Parse(ddlPosition.SelectedValue),
                                                    int.Parse(ddlDimension.SelectedValue),
                                                    int.Parse(ddlReportPart.SelectedValue),
                                                    int.Parse(ddlRegion.SelectedValue));
            txtbComment.Text = strComment;
            commentBll = null;
        }

        protected void ddlPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataupdate();
        }

        protected void ddlDimension_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataupdate();
        }

        protected void ddlReportPart_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataupdate();
        }

        protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataupdate();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Comment temp = new Comment();
            temp.Comment_nm = txtbComment.Text.Trim();
            temp.Dimension_id = int.Parse(ddlDimension.SelectedValue);
            temp.Posion_id = int.Parse(ddlPosition.SelectedValue);
            temp.Region_id = int.Parse(ddlRegion.SelectedValue);
            temp.Report_part_id = int.Parse(ddlReportPart.SelectedValue);
            commentBll.Add(temp);
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("CommentList.aspx");
        }
    }
}